package org.labuladong.前缀和技巧;

/**
 * @Auther: qingle
 * @Date: 2024/10/25-21:16
 * @Description: 前缀和
 * @version: 1.0
 */
public class 不同分数段的百分比 {

		public static void main(String[] args) {
			int[] scores = {55, 90, 78, 65, 85, 76, 90, 59, 95, 80}; // 示例分数数组
			int[] boundaries = {60, 70, 80, 90, 100}; // 分数段边界
			String[] gradeSegments = {"0-59", "60-69", "70-79", "80-89", "90-100"}; // 分数段名称

			int n = scores.length;
			int[] prefixSum = new int[boundaries.length + 1]; // 前缀和数组

			// 填充前缀和数组
			for (int score : scores) {
				for (int i = 0; i < boundaries.length; i++) {
					if (score < boundaries[i]) {
						prefixSum[i]++;
						break;
					}
					if (score >= boundaries[boundaries.length - 1]) {
						prefixSum[boundaries.length]++;
					}
				}
			}

			// 计算并打印每个分数段的百分比
			for (int i = 0; i < gradeSegments.length; i++) {
				int count = prefixSum[i];
				System.out.println(count);
				double percentage = (count * 100.0) / n;
				System.out.printf("Percentage of students in the %s segment: %.2f%%\n", gradeSegments[i], percentage);
			}
		}
}
